﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Medianamik.Core;
using DAL = Medianamik.SubsonicDALProvider.DAL ;
using System.Data;

namespace Medianamik.SubsonicDALProvider
{
    /// <summary>
    /// Classe permettant la conversion d'objets du DAL SubSonic en objets du BOL
    /// </summary>
   internal static class SubsonicConverter
   {
       /// <summary>
       /// Converti un objet PropertyX du DAL SubSonic en NodeTypeProperty
       /// </summary>
       /// <param name="pDALProperty">L'objet PropertyX à convertir</param>
       /// <returns>Un objet de type NodeTypeProperty</returns>
       public static NodeTypeProperty FromDALProperty(DAL.PropertyX pDALProperty)
       {
           NodeTypeProperty result = new NodeTypeProperty(pDALProperty.Name,
               pDALProperty.IsLanguageNeutral,
               pDALProperty.AllowNulls,
               pDALProperty.DataTypeId,
               pDALProperty.GroupName,
               pDALProperty.IsUnique);

           result.Length = pDALProperty.Length;
           result.Flags = pDALProperty.Flags;
           result.ID = pDALProperty.PropertyId;

           return result;
       }

       /// <summary>
       /// Converti un objet NodeTypeProperty en PropertyX du DAL SubSonic
       /// </summary>
       /// <param name="pProperty">L'objet NodeTypeProperty à convertir</param>
       /// <returns>Un objet de type PropertyX</returns>
       public static DAL.PropertyX ToDALProperty(NodeTypeProperty pProperty)
       {
           DAL.PropertyX result = new DAL.PropertyX()
           {
               PropertyId = pProperty.ID.GetValueOrDefault(Guid.Empty),
               TypeId = pProperty.TypeID.GetValueOrDefault(Guid.Empty),
               GroupName = pProperty.GroupName,
               DataTypeId = pProperty.DataTypeID,
               Flags = pProperty.Flags,
               IsLanguageNeutral = pProperty.IsLanguageNeutral,
               IsUnique = pProperty.IsUnique,
               Length = pProperty.Length,
               AllowNulls = pProperty.AllowsNull,
               IsNew = pProperty.IsNew
           };

           return result;

       }

       /// <summary>
       /// Converti un objet Type du DAL SubSonic en NodeType, avec ses Properties
       /// </summary>
       /// <param name="pDALType">L'objet Type à convertir</param>
       /// <param name="pDALProperties">La collection de PropertyX associée au Type</param>
       /// <returns>Un objet de type NodeType, avec sa collection de NodeTypeProperties</returns>
       public static NodeType FromDALType(DAL.Type pDALType, List<DAL.PropertyX> pDALProperties)
       {
           //Check input
           foreach (DAL.PropertyX prop in pDALProperties)
           {
               if (!prop.TypeId.Equals(pDALType.TypeId))
               {
                   throw new Exception("Properties TypeID must be the same as Type TypeID");
               }
           }

           NodeType result = new NodeType(pDALType.TypeId, pDALType.Name, pDALType.CanHaveChilds, pDALType.ParentTypeId);

           result.Properties.AddRange(pDALProperties.ConvertAll<NodeTypeProperty>(p => FromDALProperty(p)));
           return result;
       }

       /// <summary>
       /// Converti un objet de type NodeType en Type du DAL SubSonic
       /// </summary>
       /// <param name="pType">L'objet de type NodeType à convertir</param>
       /// <param name="pDALProperties">Paramètre de sortie.
       /// Permet de récupérer la collection de PropertyX du Type Medianamik</param>
       /// <returns>Un objet de type Type du DAL SubSonic</returns>
       public static DAL.Type ToDALType(NodeType pType, out DAL.PropertyXCollection pDALProperties)
       {
           DAL.Type result = new DAL.Type()
           {
              TypeId = pType.ID.GetValueOrDefault(Guid.Empty),
              ParentTypeId = pType.ParentID,
              CanHaveChilds = pType.CanHaveChilds ,
              Name = pType.Name ,
           };

           pDALProperties = new DAL.PropertyXCollection();
           pDALProperties.AddRange(pType.Properties.ConvertAll<DAL.PropertyX>(p => ToDALProperty(p)));

           return result;

       }

       //TODO : Ajouter les propriétés et leurs valeurs
       public static Core.Node FromDALNode(DAL.Node pDALNode, DataTable pDALProperties)
       {
           Core.Node node = new Core.Node(pDALNode.NodeId, pDALNode.TypeId, pDALNode.ParentNodeId);

           //Check input
           foreach (DataRow property in pDALProperties)
           {
               if (property[DAL.PropertyX.Columns.TypeId].Equals(pDALNode.TypeId))
               {
                   node.Propeties.
               }
               else
               {
                   throw new Exception("Properties TypeID must be the same as Type TypeID");
               }
           }

           return node;
       }
   }
}
